package gant.ext

import org.codehaus.gant.GantBinding
import org.codehaus.gant.GantState
import org.codehaus.gant.GantBuilder

/**
 * Maintains a global instance of the ant builder so logging is properly performed
 */
class AntBuilderFactory {
    final GantBuilder builder
    static AntBuilderFactory _instance

    private AntBuilderFactory(GantBuilder builder) {
        GantState.metaClass.static.setLoggingLevel = {int level ->
            GantState.verbosity = level
            builder.logger.messageOutputLevel = level
        }
        this.builder = builder
    }

    def static createInstance(GantBuilder builder) {
        assert null == _instance: "AntBuilderFactory has aleady been created"

        _instance = new AntBuilderFactory(builder)
    }

    static AntBuilderFactory getInstance() {
        assert null != _instance: "AntBuilderFactory has not been created yet"
        return _instance
    }

    static boolean builderFactoryExists() {
        return _instance != null
    }

    static reset() {
        _instance = null
    }
}
